/*** Calculates various statistics cited in the paper ***/

clear all
set more 1

do "paths.do"

capture log close
log using "$LOGFILES\tracking_11.log", replace

/************************************************************
Stats Cited: KAPPA
Stats on special education restrictive placements and
relationship to missing test scores.
*************************************************************/

use "$WORKING\tracking_6-math.dta", clear

tab grade

keep if (grade >= 4) & (grade <= 8)

tab grade Pspedr
tab Pdis_phy Pspedr
tab Pdis_oth Pspedr

gen tmp1 = zLMscore == .

sum tmp1
sum zLMscore

sum tmp1 if Pspedr
sum zLMscore if Pspedr

/************************************************************
Peer dispersion
*************************************************************/

use "$WORKING\tracking_7.dta", clear

keep if (subj == "math")
keep campus grade year schsz zt* CGa*

gen trk_sch_fa = ztrackR
gen trk_sch_mr = (ztrackR - ztrackRavg) / (ztrackRavg_max - ztrackRavg)

reg CGavgclssd trk_sch_fa
reg CGavgclssd trk_sch_mr

/**********************************************************
Stats Cited: IOTA
Number of students who are "topcoded" - fraction of
students in top bin
**********************************************************/

use "$WORKING\tracking_1-dist.dta", clear 

egen gy_obs = sum(nobs), by(test_grade test_year)
egen gy_max_score = max(score), by(test_grade test_year)

keep if score == gy_max_score
gen gy_pct_top = nobs / gy_obs

egen state_obs = sum(gy_obs)
egen topcoded_obs = sum(nobs)

gen state_pct_top = topcoded_obs / state_obs

gen g38 = (test_grade <= 8) & (test_grade >= 3)
egen g38_obs = sum(gy_obs * g38)
egen g38_top = sum(nobs * g38)
gen g38_pct_top = g38_top / g38_obs

sum *_pct_top

keep if g38
sum gy_pct_top
gsort -gy_pct_top

list nobs gy_obs gy_pct_top in 1/10

/**********************************************************
Number of students with generic courses functioning as
math courses and with multiple math courses
**********************************************************/

use "$WORKING\tracking_6-math", clear

tab grade Sgeneric
tab grade Smult

/************************************************************
Stats Cited: LAMBDA
Average sd of classroom peer achievement overall,
and at the 5th, 10th, 90th, and 95th percentiles of absolute
and relative tracking for all, elementary, and middle school
grades (student weighted)
*************************************************************/

use "$WORKING\tracking_7.dta", clear

keep if (subj == "math")
keep campus grade year schsz zt* CGa* CGenrl

gen trk_sch_fa = ztrackR
gen trk_sch_mr = (ztrackR - ztrackRavg) / (ztrackRavg_max - ztrackRavg)

gen is_elem = (grade <= 5)
gen is_mid = (grade >= 6)
gen is_all = 1

tabstat CGavgclssd [weight=CGenrl], statistics(mean)
tabstat CGavgclssd [weight=CGenrl], by(grade) statistics(mean)
tabstat CGavgclssd [weight=CGenrl], by(is_elem) statistics(mean)

/************************************************************
Stats Cited: NU
Coefficients from regression of lagged math z-score on
(poc), (frpl), and school-grade-year FEs.
*************************************************************/

** Sample to use
use campus distnum0 grade year Sservicex Sclass_id LMscore zLMscore Dblack Dhisp Pdisadv id1 using "$WORKING\tracking_6-math", clear
keep if (grade >= 4) & (grade <= 8)

** Target variables
gen D_poc = Dblack | Dhisp
gen D_frpl = (Pdisadv > 0)
gen D_zsco = zLMscore
foreach vv in zsco poc frpl {
    gen D_`vv'2 = D_`vv' ^ 2
	gen has_`vv' = (D_`vv' != .)
}

gen samp0 = has_zsco & has_poc & has_frpl

egen cgy = group(distnum0 campus grade year)

reghdfe D_zsco D_poc, old absorb(cgy) vce(robust)
reghdfe D_zsco D_frpl, old absorb(cgy) vce(robust)
reghdfe D_zsco D_poc D_frpl, old absorb(cgy) vce(robust)

log close

	
	
	